package datastructure.link;

/**
 * @author XY
 * @version 1.0.0
 * @ClassName LC203.java
 * @createTime 2025年07月30日 21:09:00
 */
public class LC203 {
    public ListNode removeElements1(ListNode head, int val) {
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode temp = pre;
        while (temp.next != null) {
            if (temp.next.val == val) {
                temp.next = temp.next.next;
            } else {
                temp = temp.next;
            }
        }
        return pre.next;
    }

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }
}
